const Base = require('./base.js');

module.exports = class extends Base {
  constructor(ctx) {
		super(ctx);
		this.specification = this.model('store/specification');
		
	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.userid)) {
			where.user_id = posts.userid;
		}
		const data = await this.specification.dataTable(posts, where);
		console.log(JSON.stringify(data));
		return this.json(data);
	}
	
	async addAction() {
		if(this.isGet) {

			return this.display();
		}

		let posts = this.post();
		let specification_id = await this.specification.add({
			name: posts.name
		});
		if(specification_id == 0) {
			return this.fail("添加失败");
		}
		return this.success("添加成功");

	}

	async editAction() {
		if(this.isGet) {
			let id = this.get("id");
			let specification = await this.specification.findOne(id);
			this.assign("specification", specification);
			return this.display();
		}

		let posts = this.post();
		let rows = await this.specification.setOne(posts.id,{
			name: posts.name
		});
		if(rows == 0) {
			return this.fail("修改失败");
		}

		

		return this.success("修改成功");
	}
};
